﻿using System;
using System.Collections.Generic;
using System.Text;
using UnityEngine;

[Serializable]
public class LuaData : ScriptableObject
{
    public List<string> codes;
    public List<string> names;
}

public class ResData
{
    public string mDataMD5;
    public string mDataPath;
    public uint mSize;

    public override string ToString()
    {
        return string.Format("{0} {1} {2}\n", mDataPath, mDataMD5, mSize);
    }
}

public class FileData
{
    public List<string> files = new List<string>();
    public List<ResData> resData = new List<ResData>();
    private StringBuilder sb = new StringBuilder();

    public void AddResData(ResData data)
    {
        if (!files.Contains(data.mDataPath))
        {
            files.Add(data.mDataPath);
            resData.Add(data);
        }
    }

    public void RemoveResData(ResData data)
    {
        int idx = files.IndexOf(data.mDataPath);
        if (idx > -1)
        {
            files.RemoveAt(idx);
            resData.RemoveAt(idx);
        }
    }

    public override string ToString()
    {
        sb.Length = 0;
        for (int idx = 0; idx < resData.Count; ++idx)
        {
            sb.Append(resData[idx].ToString());
        }
        return sb.ToString();
    }

    public void ReadBytes(Byte[] bytes)
    {
        string content = Encoding.UTF8.GetString(bytes);
        string[] lines = content.Split('\n');
        for (int idx = 0; idx < lines.Length; ++idx)
        {
            string line = lines[idx];
            if (line == "") { continue; }
            string[] info = line.Split(' ');
            files.Add(info[0]);

            ResData rd = new ResData();
            rd.mDataPath = info[0];
            rd.mDataMD5 = info[1];
            rd.mSize = uint.Parse(info[2]);
            resData.Add(rd);
        }
    }
}